/**
 * Created by cuppi on 2017/10/16.
 */
'use strict'
import NetworkTrade from '@/network/NetworkTrade'
let instance = null;
class TradeManager {
  constructor() {
    if (!instance) {
      instance = this;
    }
    return instance;
  }

  /**
   * 获取单例实例
   * @returns {TradeManager}
   */
  static defaultManager() {
    return new TradeManager();
  }

  /**
   * 获取交易商务参数（与座位无关的）
   * @param platform 平台类型
   * @param filmId 电影Id
   * @param cinemaId 影院Id
   * @returns {*} 商务参数
   */
  static tradeParasFromPlatform(platform, filmId, cinemaId) {
    let {showId, type} = platform;
    return {type, showId, jbzCinemaId: cinemaId, jbzFilmId: filmId};
  }

  /**
   * 获取锁座处理者
   * @param type 平台类型
   * @param paras 参数
   * @returns {*} 返回请求promise
   */
  lockSeatHandlerFrom(type, paras) {
    return NetworkTrade.useParas('openId').tradeLockSeat(type, paras);
  }

  // TODO: 取消type
  /**
   * 获取下订单处理者
   * @param type 平台类型
   * @param paras 参数
   * @returns {{terminate, then}|*} 返回请求promise
   */
  confirmOrderHandlerFrom(paras) {
    return NetworkTrade.tradeApplyOrderNeedLogin('', paras);
  }

  /**
   * 获取锁座时需要的座位参数
   * @param type 平台类型
   * @param seatList 座位列表（需要购买的）
   * @param mobile 手机号码
   * @param custId 身份标识符（目前跟手机号码一样）
   * @returns {*} 返回参数对象
   */
  seatInforParas(type, seatList, mobile, custId) {
    if (type === 'taobao') {
      let seatInfos = [];
      let seatNumberInfos = [];
      let seatsName = [];
      for (let seat of seatList) {
        seatInfos.push(seat.seatModel.seatId);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
        seatsName.push(seat.seatModel.name);
      }
      return {
        seatIds: seatInfos.join('|'),
        count: seatInfos.length,
        seatNumberInfos: seatNumberInfos.join('|'),
        seatsName: seatsName.join('|'),
        mobile,
        custId
      }
    }
    // 网票
    if (type === 'wangpiao') {
      let seatInfos = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        seatInfos.push(seat.seatModel.SeatIndex);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      return {
        seatIds: seatInfos.join('|'),
        count: seatInfos.length,
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }
    // 蜘蛛
    if (type === 'spider') {
      let seatInfos = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        seatInfos.push(seat.seatModel.rowId + ':' + seat.seatModel.columnId);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      return {
        seatIds: seatInfos.join('|'),
        count: seatInfos.length,
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }
    // 卖座
    if (type === 'maizuo') {
      let seatInfos = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        seatInfos.push(seat.seatModel.rowId + ':' + seat.seatModel.columnId);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      return {
        seatIds: seatInfos.join('|'),
        count: seatInfos.length,
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }
    // 单车
    if (type === 'danche') {
      let seatInfos = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        seatInfos.push(seat.seatModel.rowId + ':' + seat.seatModel.columnId);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      return {
        seatIds: seatInfos.join('|'),
        count: seatInfos.length,
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }
    // 猫眼
    if (type === 'maoyan' || type === 'meituan' || type === 'dazhong') {
      let seatInfos = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        if (!seat.seatModel) {
          console.log(seat);
        }
        seatInfos.push({
          sectionId: seat.seatModel.sectionId,
          columnId: seat.seatModel.columnId,
          rowId: seat.seatModel.rowId,
          seatNo: seat.seatModel.seatNo
        });
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      return {
        seatIds: JSON.stringify({
          count: seatList.length,
          list: seatInfos
        }),
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }

    // 百度
    if (type === 'baidu') {
      let seatIds = [];
      let seatNumberInfos = [];
      for (let seat of seatList) {
        seatIds.push(seat.seatModel.seatNo);
        seatNumberInfos.push(seat.rowNumber + ':' + seat.colNumber);
      }
      console.log('peng' + JSON.stringify(seatList))
      return {
        areaInfo:seatList.areaInfo,
        count: seatIds.length,
        //  后台设置特意反过来的
        seatIds: seatIds.join('|'),
        seatNumberInfos: seatNumberInfos.join('|'),
        mobile,
        custId
      }
    }
  }

  /**
   * 购票（执行锁座 下订单 的事务）
   * @param tradeParas 商务参数（与座位无关的参数集合）
   * @param seatList 座位列表
   * @param mobile 手机号码
   * @param openId 身份标识符（同上）
   * @returns {*} 返回请求promise
   */
  buyTicket(tradeParas, seatList, mobile, openId) {
    let type = tradeParas.type;
    let paras = {...tradeParas, ...this.seatInforParas(type, seatList, mobile, openId)};
    let bridgeSelf = this;
    return {
      next: function (lockSeatCallback) {
        return {
          next: function (confirmOrderCallback) {
            bridgeSelf.lockSeatHandlerFrom(type, {...paras, cinemaId: paras.platformCinemaId}).then(orderId => {
              lockSeatCallback(null, orderId);
              let {cinemaId, jbzFilmId, filmId, showId} = paras;
              bridgeSelf.confirmOrderHandlerFrom({
                custId: openId,
                orderId,
                cinemaId,
                jbzFilmId,
                filmId,
                showId
              }).then(data => {
                confirmOrderCallback(null, data);
              }, error => {
                confirmOrderCallback(error, null);
              });
            }, error => {
              lockSeatCallback(error, null);
            });
          }
        }
      }
    }
  }

  /**
   * 锁座
   * @param tradeParas 商务参数（与座位无关的参数集合）
   * @param seatList 座位列表
   * @param mobile 手机号码
   * @returns {Promise} 返回请求promise
   */
  lockSeat(tradeParas, seatList, mobile, openId) {
    let type = tradeParas.type;
    let paras = {...tradeParas, ...this.seatInforParas(type, seatList, mobile, openId)};
    return new Promise((resolve, reject) => {
      this.lockSeatHandlerFrom(type, {...paras, cinemaId: paras.platformCinemaId}).then(orderId => {
        resolve(orderId);
      }, error => {
        reject(error);
      });
    })
  }

  /**
   * 确认订单
   * @param type 平台类型
   * @param lockSeatResultData 锁座成功返回的数据 {orderId:orderId}
   * @returns {Promise}
   */
  applyOrder(lockSeatResultData) {
    console.log(lockSeatResultData);
    return new Promise((resolve, reject) => {
      this.confirmOrderHandlerFrom(lockSeatResultData).then(data => {
        resolve(data);
      }, error => {
        reject(error);
      });
    });
  }

}

export default TradeManager;
